/**
 * @fileOverview 菜单
 * @author goodzsq@gmail.com
 */
var BaseClass = gg.module('ui.jq.Component');
module.exports = gg.createClass({
    attributes: {
        items: {
            type: 'datatype.Array',
            defaultValue: [],
            setter: function(v) {
                this.defaultSetter('items', v);
                this.refresh();
            }
        }
    },
    methods: {
        refresh: function() {
            if(!this.inited){
                return;
            }
            var items = this.get('items');
            this.el.children().remove();
            for (var i = 0; i < items.length; i++) {
                if(gg.type(items[i])==='object'){
                    if(items[i]['disabled']){
                        this.el.append('<li class="disabled"><a class="menuitem">' + items[i]['title'] + '</a></li>');
                        
                    }else{
                        this.el.append('<li><a class="menuitem" href="#">' + items[i]['title'] + '</a></li>');
                    }
                }else if(items[i] === '-'){
                    this.el.append('<li class="divider"></li>');
                }else{
                    this.el.append('<li class="dropdown-header">'+items[i]+'</li>');
                }
            }
        },
        /**
         * @override
         */
        createElement: function() {
            this.el = $('<ul id="' + this.cid + '" class="ui dropdown-menu">');
            var self = this;
            this.el.delegate('>li>a', 'click', function() {
                var items = self.get('items');
                var idx = $(this).parent().index();
                if(items[idx] && items[idx].callback && !items[idx].disabled){
                    self.el.css('display', 'none');
                    items[idx].callback.call(self);
                }
            });
        },
        show: function(x, y) {
            this.el.css({display: 'block', left: x, top: y});
        }
    },
    events: {
    }
}, BaseClass, []);